<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>实例管理</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
    <script src="/qrcode/jquery-3.5.1.js"></script>
    <style>
        .pear-container {
            margin: 15px;
            width: calc(100vw - 30px);
        }

        .layui-table-box button {
            font-size: 14px !important;
            height: 25px;
            line-height: 23px;
        }

        .layui-form-item .layui-inline {
            margin-bottom: 10px;
        }

        .layui-form-label {
            padding: 9px 8px;
            width: 60px;
        }
    </style>
</head>
<body class="pear-container">
<div class="layui-card searchCriteria">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">连接名称</label>
                    <div class="layui-input-inline">
                        <select name="nodeId">
                            <option value="" selected>请选择</option>
                            <option th:each="item : ${node}" th:value="${item.id}" th:text="${item.name}"></option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">备份来源</label>
                    <div class="layui-input-inline">
                        <select name="category">
                            <option value="" selected>请选择</option>
                            <option value="手动备份">手动备份</option>
                            <option value="自动备份">自动备份</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">备份状态</label>
                    <div class="layui-input-inline">
                        <select name="status">
                            <option value="" selected>请选择</option>
                            <option value="0">备份中</option>
                            <option value="1">上传中</option>
                            <option value="2">成功</option>
                            <option value="-1">失败</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">数据库名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="databaseName" autocomplete="off" class="layui-input"
                               placeholder="请输入">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">数据表名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="tablesName" autocomplete="off" class="layui-input"
                               placeholder="请输入">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit
                            lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="mysqlTable" lay-filter="mysqlTable"></table>
    </div>
</div>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script type="text/javascript" th:inline="javascript">
    let nginxPath = [[${nginxPath}]];
    layui.use(['jquery', 'form', 'table', 'layer'], function () {
        const form = layui.form,
            table = layui.table,
            layer = layui.layer;
        let cols = [
            [
                {
                    field: 'category', title: '备份来源', align: 'center', templet: function (d) {
                        if ('手动备份' == d.category) {
                            return '<span class="badge-custom-1">' + d.category + '</span>';
                        } else {
                            return '<span class="badge-custom-2">' + d.category + '</span>';
                        }
                    }
                },
                {field: 'node_name', title: '连接名称', align: 'center'},
                {field: 'database_name', title: '数据库名', align: 'center'},
                {field: 'tables_name', title: '数据表名', align: 'center'},
                {
                    field: 'data_type', title: '备份方式', align: 'center', templet: function (d) {
                        if (0 == d.data_type) {
                            return '表结构';
                        }
                        if (1 == d.data_type) {
                            return '表数据';
                        }
                        return '表结构+表数据';
                    }
                },
                {
                    field: 'status', title: '备份状态', align: 'center', templet: function (d) {
                        if (0 == d.status) {
                            return '<span class="badge-custom-2">备份中</span>'
                        }
                        if (1 == d.status) {
                            return '<span class="badge-custom-2">上传中</span>'
                        }
                        if (2 == d.status) {
                            return '<span class="badge-custom-1">成功</span>'
                        }
                        if (-1 == d.status) {
                            return '<span class="badge-custom-3">失败</span>'
                        }
                    }
                },
                {
                    field: 'file_size', title: '文件大小', align: 'center', templet: function (d) {
                        let size = parseFloat(d.file_size / 1024).toFixed(2)
                        if (size < 1024) {
                            return size + ' KB';
                        } else if (1024 <= size && size < 1024 * 1024) {
                            return parseFloat(size / 1024).toFixed(2) + 'MB';
                        } else {
                            return parseFloat(size / 1024 / 1024).toFixed(2) + 'GB';
                        }
                    }
                },
                {field: 'start_time', minWidth: 180, title: '开始时间', align: 'center'},
                {
                    field: 'spend_time', title: '耗时', align: 'center', templet: function (d) {
                        let time = d.spend_time;
                        // ms
                        if (0 <= time && time < 1000) {
                            return time + '毫秒';
                        }
                        // s
                        else if (1000 <= time && time < 60 * 1000) {
                            return parseFloat(time / 1000).toFixed(2) + '秒';
                        }
                        // minute
                        else if (60 * 1000 <= time && time < 60 * 60 * 1000) {
                            return (time / 1000 / 60).toFixed(0) + '分' + ((time / 1000) % 60).toFixed(2) + '秒';
                        }
                        // hour
                        else {
                            return (time / 1000 / 60 / 60).toFixed(2) + '小时'
                        }
                    }
                },
                {
                    title: '操作', minWidth: 150, fixed: 'right', templet(d) {
                        if (d.status == 2) {
                            return ' <button class="pear-btn pear-btn-primary pear-btn-sm" style="background-color: #398FFF !important;' +
                                '                              border: 1px solid #398FFF !important;" lay-event="download">下载</button>' +
                                ' <button class="pear-btn pear-btn-primary pear-btn-sm" style="background-color: #40bb9c !important;' +
                                    ' border: 1px solid #40bb9c !important;" lay-event="recovery">恢复</button>';
                        } else {
                            return '';
                        }
                    }
                }
            ]
        ]

        tableIns = table.render({
            elem: '#mysqlTable',
            url: '/backup/backup/log/list',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports'],
            id: 'mysqlTable',
            height: 'full-173',
            //解决页码的问题
            done: function (res) {
                // 获得当前页码
                var brforeCurr = tableIns.config.page.curr;
                // 获得当前页的记录数
                var dataLength = table.cache['mysqlTable'].length;
                var count = res.count; // 获得总记录数
                //如果当前页的记录数为0并且总记录数不为0
                if (dataLength == 0 && count != 0) {
                    // 刷新表格到上一页
                    table.reload("mysqlTable", {
                        page: {
                            curr: brforeCurr - 1
                        }
                    });
                }
            }
        });

        table.on('tool(mysqlTable)', function (obj) {
            if (obj.event === 'recovery') {
                window.recovery(obj);
            } else if (obj.event === 'download') {
                window.download(obj);
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });

        table.on('toolbar(mysqlTable)', function (obj) {
            if (obj.event === 'refresh') {
                window.refresh();
            }
        });


        window.recovery = function (obj) {
            layer.confirm('确认恢复备份吗？', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                $.ajax({
                    url: '/backup/backup/log/importsql',
                    type: 'get',
                    data: {id: obj.data.id},
                    dataType: 'json',
                    contentType: "application/json;charset=utf-8",
                    success: function (res) {
                        if (res.code === 200) {
                            layer.msg(res.message, {icon: 1});
                            table.reload('mysqlTable');
                        } else {
                            layer.msg(res.message, {icon: 2});
                        }
                    }
                });
            });
        }

        window.download = function (obj) {
            console.log(nginxPath + obj.data.file_path);
            window.open(nginxPath + obj.data.file_path, '_blank');
        }


        window.refresh = function (param) {
            table.reload('mysqlTable');
        }

        form.on('submit(user-query)', function (data) {
            table.reload('mysqlTable', {
                where: data.field,
                page: {
                    curr: 1 // 重新从第 1 页开始
                }
            });
            return false;
        });

    })
</script>
</body>
</html>